package com.unittec.drainage.provider.utils;


import cn.hutool.core.util.ObjectUtil;
import com.unittec.drainage.sdk.root.domain.TreeNode;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @author: chenjiawang
 * @CreateDate: 2020/2/3 18:27
 */
public class TreeUtil {


    public static <T extends TreeNode> List<T> tree(List<T> treeNodes, String rootId) {
        for (TreeNode t : treeNodes) {
            if (t.getParentId() == null) {
                t.setParentId("");
            }
        }
        if (rootId == null) {
            rootId = "";
        }
        Map<String, List<TreeNode>> collect = treeNodes.stream().collect(Collectors.groupingBy(TreeNode::getParentId));

        for (TreeNode projectFolderTreeDto : treeNodes) {
            projectFolderTreeDto.setChildren(collect.getOrDefault(projectFolderTreeDto.getId(), new ArrayList<>()));
        }
        String finalParentId = rootId;
        return treeNodes.stream()
                .filter(projectFolderTreeDto -> finalParentId.equals(projectFolderTreeDto.getId()))
                .collect(Collectors.toList());
    }

    /*
     * 将树结构转化为list结构
     * */
    public static <T extends TreeNode> void  treeToList(List<T> rootNode,List<T> listNode) {

        rootNode.stream().forEach(e->{
            listNode.add(e);
            if(ObjectUtil.isNotEmpty(e.getChildren())){
                treeToList((List<T>) e.getChildren(),listNode);
            }
        });
    }

    /**
     * * 两层循环实现建树
     * *
     * * @param treeNodes 传入的树节点列表
     * * @return
     */
    public static <T extends TreeNode> List<T> bulid(List<T> treeNodes, String rootId) {

        List<T> trees = new ArrayList<>();

        for (T treeNode : treeNodes) {
            if (StringUtils.isBlank(treeNode.getParentId()) || treeNode.getId().equals(rootId)) {
                trees.add(treeNode);
            }
            for (T it : treeNodes) {
                if (null != it.getParentId() && it.getParentId().equals(treeNode.getId())) {
                    if (treeNode.getChildren() == null) {
                        treeNode.setChildren(new ArrayList<>());
                    }
                    treeNode.add(it);
                }
            }
        }
        return trees;
    }


    /**
     * 使用递归方法建树
     *
     * @param treeNodes
     * @return
     */
    public static <T extends TreeNode> List<T> buildByRecursive(List<T> treeNodes, String rootId) {
        List<T> trees = new ArrayList<T>();
        for (T treeNode : treeNodes) {
            if (null == treeNode.getParentId() && treeNode.getParentId().equals(rootId)) {
                trees.add(findChildren(treeNode, treeNodes));
            }
        }
        return trees;
    }

    /**
     * 递归查找子节点
     *
     * @param treeNodes
     * @return
     */
    public static <T extends TreeNode> T findChildren(T treeNode, List<T> treeNodes) {
        for (T it : treeNodes) {
            if (treeNode.getId().equals(it.getParentId())) {
                if (treeNode.getChildren() == null) {
                    treeNode.setChildren(new ArrayList<>());
                }
                treeNode.add(findChildren(it, treeNodes));
            }
        }
        return treeNode;
    }
}
